package com.shrct.businessprocess.project.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.project.NumberRange;
import com.shrct.businessprocess.project.RCSalePayStrategy;
import com.shrct.businessprocess.project.RCSalePayStrategyDAO;
import com.shrct.businessprocess.project.events.RCSalePayStrategyEventsManager;
import com.shrct.businessprocess.project.impl.RCSalePayStrategyImpl;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseDAOService;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.project.RCSalePayStrategyDAO")
public class RCSalePayStrategyDAOImpl extends BaseDAOService<RCSalePayStrategy> implements RCSalePayStrategyDAO {
  @Autowired
  private RCSalePayStrategyEventsManager eventsManager;
  
  public RCSalePayStrategy create(final Procedure1<? super RCSalePayStrategy> init) {RCSalePayStrategy rCSalePayStrategy=RCSalePayStrategyImpl.create(getObjectFactory());
    final Procedure1<RCSalePayStrategy> _function = new Procedure1<RCSalePayStrategy>() {
        public void apply(final RCSalePayStrategy it) {
          init.apply(it);
          RCSalePayStrategyDAOImpl.this.eventsManager.fireCreated(it);
        }
      };
    RCSalePayStrategy _doubleArrow = ObjectExtensions.<RCSalePayStrategy>operator_doubleArrow(rCSalePayStrategy, _function);
    return _doubleArrow;
  }
  
  public Boolean allRangeCovered() {
    boolean _xblockexpression = false;
    {IGenericQuery<RCSalePayStrategy> query=getObjectFactory().createGenericQuery(RCSalePayStrategy.class,"select e from com.shrct.businessprocess.project.impl.RCSalePayStrategyImpl as e   where  e.active = 1  order by e.range.fromValue ASC    ");
      final Collection<RCSalePayStrategy> strategies = query.list();
      final Function1<RCSalePayStrategy,NumberRange> _function = new Function1<RCSalePayStrategy,NumberRange>() {
          public NumberRange apply(final RCSalePayStrategy it) {
            NumberRange _range = it.getRange();
            return _range;
          }
        };
      Iterable<NumberRange> _map = IterableExtensions.<RCSalePayStrategy, NumberRange>map(strategies, _function);
      final Function2<NumberRange,NumberRange,NumberRange> _function_1 = new Function2<NumberRange,NumberRange,NumberRange>() {
          public NumberRange apply(final NumberRange r1, final NumberRange r2) {
            NumberRange _merge = r1.merge(r2);
            return _merge;
          }
        };
      final NumberRange fullRange = IterableExtensions.<NumberRange>reduce(_map, _function_1);
      boolean _xifexpression = false;
      boolean _or = false;
      boolean _or_1 = false;
      boolean _equals = Objects.equal(fullRange, null);
      if (_equals) {
        _or_1 = true;
      } else {
        Double _fromValue = fullRange.getFromValue();
        boolean _notEquals = ((_fromValue).doubleValue() != 0);
        _or_1 = (_equals || _notEquals);
      }
      if (_or_1) {
        _or = true;
      } else {
        Double _toValue = fullRange.getToValue();
        boolean _notEquals_1 = (!Objects.equal(_toValue, null));
        _or = (_or_1 || _notEquals_1);
      }
      if (_or) {
        _xifexpression = false;
      } else {
        _xifexpression = true;
      }
      _xblockexpression = (_xifexpression);
    }
    return Boolean.valueOf(_xblockexpression);
  }
  
  public Double calcCom2Amount(final Double totalAmount, final Double comparableIncome) {
    Double _xblockexpression = null;
    {IGenericQuery<RCSalePayStrategy> query=getObjectFactory().createGenericQuery(RCSalePayStrategy.class,"select e from com.shrct.businessprocess.project.impl.RCSalePayStrategyImpl as e   where  e.active = 1  order by e.range.fromValue ASC    ");
      final Collection<RCSalePayStrategy> strategies = query.list();
      final Function1<RCSalePayStrategy,Double> _function = new Function1<RCSalePayStrategy,Double>() {
          public Double apply(final RCSalePayStrategy it) {
            Double _calcCom2Amount = it.calcCom2Amount(totalAmount, comparableIncome);
            return _calcCom2Amount;
          }
        };
      Iterable<Double> _map = IterableExtensions.<RCSalePayStrategy, Double>map(strategies, _function);
      final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
      Double _nullsafe = NumberExtensions.nullsafe(_reduce);
      _xblockexpression = (_nullsafe);
    }
    return _xblockexpression;
  }
  
  public void delete(final RCSalePayStrategy value) {getObjectFactory().delete(value);
    
    this.eventsManager.fireDeleted(value);
  }
}
